package pers.sports.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pers.sports.service.ApplyService;
import pers.sports.service.StudentService;
import pers.sports.vo.ApplyDetailed;
import pers.sports.vo.StorageUser;
import pers.sports.vo.VolunteerInfo;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报名信息处理
 *
 * @datetime 2021/4/25 15:46
 */
@RestController
@RequestMapping("/apply")
public class ApplyController {

    private final ApplyService applyService;
    private final StudentService studentService;

    public ApplyController(ApplyService applyService, StudentService studentService) {
        this.applyService = applyService;
        this.studentService = studentService;
    }


    @RequestMapping("findApply")
    public Map<String, Object> findApply(Integer page, Integer limit) {
        Map<String, Object> map = new HashMap<>();
        List<ApplyDetailed> list = applyService.findApplyInfo(page, limit, 5);
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", list.size());
        map.put("data", list);
        return map;
    }

    @RequestMapping("findApplyPass")
    public Map<String, Object> findApplyPass(Integer page, Integer limit) {
        Map<String, Object> map = new HashMap<>();
        List<ApplyDetailed> list = applyService.findApplyInfo(page, limit, 1);
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", list.size());
        map.put("data", list);
        return map;
    }

    @RequestMapping(value = "/updateReview")
    public String updateReview(Integer id, Integer review) {
        return applyService.updateReview(id, review) ? "success" : "err";
    }

    @RequestMapping(value = "/updateReviewList")
    public String updateReviewList(@RequestParam("id[]") Integer[] id, Integer review) {
        for (int i = 0; i < id.length; i++) {
            if (!applyService.updateReview(id[i], review)) {
                return "err";
            }
        }
        return "success";

    }

    @RequestMapping("findSelfApply")
    public Map<String, Object> findSelfApply(HttpServletRequest request) {
        StorageUser user = (StorageUser) request.getSession().getAttribute("user");
        String stuId = user.getNum();
        Map<String, Object> map = new HashMap<>();
        List<ApplyDetailed> list = applyService.findSelfApply(studentService.findStuIdByNum(stuId));
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", list.size());
        map.put("data", list);
        return map;
    }

    @RequestMapping("delete")
    public String deleteById(@RequestParam String id){
        applyService.deleteById(id);
        return "success";
    }

    @RequestMapping("volunteer")
    public String volunteer(HttpServletRequest request){
        StorageUser user = (StorageUser) request.getSession().getAttribute("user");
        String stuId = user.getNum();
        if(applyService.findVolunteerByStuId(stuId)){
            return applyService.insertVolunteer(stuId)?"success":"失败";
        }else {
            return "您已报过名了";
        }
    }

    @RequestMapping("findSelfVolunteer")
    public String findSelfVolunteer(HttpServletRequest request){
        StorageUser user = (StorageUser) request.getSession().getAttribute("user");
        String stuId = user.getNum();
        if(!applyService.findVolunteerByStuId(stuId)){
            return "success";
        }else {
            return "err";
        }
    }

    @RequestMapping("findAllVolunteer")
    public Map findAllVolunteer(){
        Map<String, Object> volunteermap = new HashMap<>();
        List<VolunteerInfo> list = applyService.findAllVolunteer();
        volunteermap.put("code", 0);
        volunteermap.put("msg", "");
        volunteermap.put("count", list.size());
        volunteermap.put("data", list);
        return volunteermap;
    }
}
